﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCon;
using System.Data.SqlClient;

namespace yfxt
{
    public partial class cx_bqty : Form
    {
        public cx_bqty()
        {
            InitializeComponent();
        }

        DataTable dt = new DataTable();
        SqlConnection sqlcon;
        SqlDataAdapter da;
        SqlCommandBuilder scdb;
        public static int ksdm;
        public static string sqrq1, sqrq2;
        string constr = "Data Source=localhost;database=yf;uid=sa;pwd=123;timeout=60";

        #region 主窗体加载
        private void cx_bqty_Load(object sender, EventArgs e)
        {
            DataTable dt_bq = new DataTable();
            string sqlstr1 = "select ksdm,ksmc from GY_KSDM where BQSY='Y'";
            dt_bq.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlstr1, null));
            comboBox1.DataSource = dt_bq;
            comboBox1.DisplayMember = "ksmc";
            comboBox1.ValueMember = "ksdm";
        }
        #endregion

        #region 刷新，获取退药信息
        private void button1_Click(object sender, EventArgs e)
        {
            #region 获取科室代码
            sqrq1 = dateTimePicker1.Value.ToShortDateString();
            sqrq2 = dateTimePicker2.Value.ToShortDateString();
            DataTable dt_ksdm = new DataTable();
            string sqlksdm = "select ksdm from gy_ksdm where ksmc='" + comboBox1.Text.ToString().Trim() + "'";
            dt_ksdm.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlksdm, null));
            ksdm = int.Parse(dt_ksdm.Rows[0]["ksdm"].ToString());
            #endregion
            dt.Clear();
            string sql_tystr = "select b.zyh,b.brch,b.brxm,a.sqrq,c.ypmc,a.ypgg,a.yfdw,a.ypsl,a.ypjg,(a.ypsl*a.ypjg) as zjje,d.cdmc from " +
                "bq_tymx a," +
                "zy_brry b," +
                "yk_typk c, " +
                "yk_cddz d " +
                "where a.tjbz=2 " +
                "and a.zfpb=0 " +
                "and tybq='" + ksdm + "' " +
                "and a.yfsb='" + mainform.yfsb + "' " +
                "and a.ypxh=c.ypxh " +
                "and a.zyh=b.zyh " +
                "and a.ypcd=d.ypcd " +
                "and a.sqrq>='" + dateTimePicker1.Value.ToShortDateString() + "' " +
                "and a.sqrq<='" + dateTimePicker2.Value.ToShortDateString() + "' ";

            sqlcon = new SqlConnection(constr);
            sqlcon.Open();
            da = new SqlDataAdapter(sql_tystr, constr);
            scdb = new SqlCommandBuilder(da);
            try
            {
                da.Fill(dt);
                dataGridView1.Columns[0].DataPropertyName = dt.Columns[0].ToString();
                dataGridView1.Columns[1].DataPropertyName = dt.Columns[1].ToString();
                dataGridView1.Columns[2].DataPropertyName = dt.Columns[2].ToString();
                dataGridView1.Columns[3].DataPropertyName = dt.Columns[3].ToString();
                dataGridView1.Columns[4].DataPropertyName = dt.Columns[4].ToString();
                dataGridView1.Columns[5].DataPropertyName = dt.Columns[5].ToString();
                dataGridView1.Columns[6].DataPropertyName = dt.Columns[6].ToString();
                dataGridView1.Columns[7].DataPropertyName = dt.Columns[7].ToString();
                dataGridView1.Columns[8].DataPropertyName = dt.Columns[8].ToString();
                dataGridView1.Columns[9].DataPropertyName = dt.Columns[9].ToString();
                dataGridView1.Columns[10].DataPropertyName = dt.Columns[10].ToString();
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            };
        }
        #endregion

        #region 打印
        private void button3_Click(object sender, EventArgs e)
        {
            cx_bqty_crpt crpt = new cx_bqty_crpt();
            crpt.ShowDialog();
        }
        #endregion

        #region 退出
        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
